package vistas;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

import clases.Estudiante;
import clases.Seccion;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FrmBuscarRetirarEStudiante extends javax.swing.JFrame {
	private JTextField jTextField1;
	private JButton jButton1;
	private JTextField nombre;
	private JTextField apellido;
	private AbstractAction abstractAction5;
	private JButton jButton2;
	private AbstractAction abstractAction4;
	private JButton jButton4;
	private AbstractAction abstractAction3;
	private AbstractAction abstractAction2;
	private AbstractAction abstractAction1;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JTextField cedula;
	private JLabel jLabel1;
	private JPanel jPanel1;
	private JButton jButton3;
	private JPanel jPanel2;
	private Seccion seccion = null;
	private Vector<JTextField> textos;
	

	public Seccion getSeccion() {
		return seccion;
	}

	public void setSeccion(Seccion seccion) {
		this.seccion = seccion;
	}

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				FrmBuscarRetirarEStudiante inst = new FrmBuscarRetirarEStudiante();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public FrmBuscarRetirarEStudiante() {
		super();
		initGUI();
		textos = new Vector<JTextField>();
		textos.add(apellido);
		textos.add(nombre);
		textos.add(cedula);
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1, "Center");
				jTextField1.setPreferredSize(new java.awt.Dimension(97,22));
				jTextField1.setBounds(0, 0, 0, 0);
			}
			{
				jPanel2 = new JPanel();
				getContentPane().add(jPanel2);
				jPanel2.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
				jPanel2.setLayout(new AnchorLayout());
				jPanel2.setBounds(12, 223, 396, 60);
				jPanel2.setLayout(null);
				{
					jButton3 = new JButton();
					jPanel2.add(jButton3);
					jButton3.setText("jButton3");
					jButton3.setAction(getAbstractAction3());
					jButton3.setBounds(311, 18, 79, 22);
				}
				{
					jButton4 = new JButton();
					jPanel2.add(jButton4);
					jButton4.setText("jButton4");
					jButton4.setBounds(204, 18, 102, 22);
					jButton4.setAction(getAbstractAction4());
				}
				{
					jButton1 = new JButton();
					jPanel2.add(jButton1);
					jPanel2.add(getJButton2());
					jButton1.setText("jButton1");
					jButton1.setAction(getAbstractAction2());
					jButton1.setBounds(2, 18, 108, 22);
				}
			}
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1);
				AnchorLayout jPanel1Layout = new AnchorLayout();
				jPanel1.setBorder(new LineBorder(new java.awt.Color(0,0,0),1,false));
				jPanel1.setLayout(jPanel1Layout);
				jPanel1.setBounds(65, 41, 292, 151);
				{
					jLabel1 = new JLabel();
					jPanel1.add(jLabel1, new AnchorConstraint(76, 388, 162, 101, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jLabel1.setText("Cedula:");
					jLabel1.setPreferredSize(new java.awt.Dimension(84, 13));
				}
				{
					nombre = new JTextField();
					jPanel1.add(nombre, new AnchorConstraint(317, 888, 441, 556, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					nombre.setPreferredSize(new java.awt.Dimension(97,22));
				}
				{
					apellido = new JTextField();
					jPanel1.add(apellido, new AnchorConstraint(575, 888, 699, 556, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					apellido.setPreferredSize(new java.awt.Dimension(97,22));
				}
				{
					cedula = new JTextField();
					jPanel1.add(cedula, new AnchorConstraint(58, 888, 182, 556, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					cedula.setPreferredSize(new java.awt.Dimension(97,22));
				}
				{
					jLabel2 = new JLabel();
					jPanel1.add(jLabel2, new AnchorConstraint(334, 361, 418, 101, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jLabel2.setText("Nombre:");
					jLabel2.setPreferredSize(new java.awt.Dimension(76,15));
				}
				{
					jLabel3 = new JLabel();
					jPanel1.add(jLabel3, new AnchorConstraint(575, 361, 660, 104, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					jLabel3.setText("Apellido:");
					jLabel3.setPreferredSize(new java.awt.Dimension(75,15));
				}
			}
			pack();
			this.setSize(430, 339);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private AbstractAction getAbstractAction2() {
		if(abstractAction2 == null) {
			abstractAction2 = new AbstractAction("Consultar", null) {
				public void actionPerformed(ActionEvent evt) {
					if (cedula.getText().isEmpty()){
						JOptionPane.showMessageDialog(new JFrame(),"Existen camoos vacios","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
						return;			
					}
					try {
						int num = Integer.parseInt(cedula.getText());
						if( num<=0){
							JOptionPane.showMessageDialog(new JFrame(),"Ingrese  un numero valido","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
							return;
						}
						else{
							Estudiante e = seccion.mostrar_Est_Ced(num);
							if(e!=null){
								nombre.setText(e.getNombre());
								apellido.setText(e.getApellido());
								cedula.setEditable(false);
							}
							else
								JOptionPane.showMessageDialog(new JFrame(),"La cedula ingresada no esta registrada","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
							
						}
							
					}catch (NumberFormatException ex) {
						JOptionPane.showMessageDialog(new JFrame(),"Ingrese  un numero valido","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
						return;
					}
				}
			};
		}
		return abstractAction2;
	}
	
	private AbstractAction getAbstractAction3() {
		if(abstractAction3 == null) {
			abstractAction3 = new AbstractAction("Salir", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return abstractAction3;
	}
	
	private AbstractAction getAbstractAction4() {
		if(abstractAction4 == null) {
			abstractAction4 = new AbstractAction("Cancelar", null) {
				public void actionPerformed(ActionEvent evt) {
					for (Iterator iterator = textos.iterator(); iterator.hasNext();) {
						JTextField texto = (JTextField) iterator.next();
						texto.setText("");
					}
					
				}
			};
		}
		return abstractAction4;
	}
	
	private JButton getJButton2() {
		if(jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("jButton2");
			jButton2.setBounds(115, 18, 84, 22);
			jButton2.setAction(getAbstractAction5());
		}
		return jButton2;
	}
	
	private AbstractAction getAbstractAction5() {
		if(abstractAction5 == null) {
			abstractAction5 = new AbstractAction("Retirar", null) {
				public void actionPerformed(ActionEvent evt) {
					if (cedula.getText().isEmpty()){
						JOptionPane.showMessageDialog(new JFrame(),"Existen camoos vacios","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
						return;			
					}
					try {
						int num = Integer.parseInt(cedula.getText());
						if( num<=0){
							JOptionPane.showMessageDialog(new JFrame(),"Ingrese  un numero valido","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
							return;
						}
						else{
							int i = seccion.eliminarEstudiante(num);
							if(i!=-1)
								JOptionPane.showMessageDialog(new JFrame(),"Estudiante eliminado exitosamente","Buscar y Retirar Estudiante",JOptionPane.INFORMATION_MESSAGE);	
							else
								JOptionPane.showMessageDialog(new JFrame(),"La cedula ingresada no esta registrada","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
							
						}
							
					}catch (NumberFormatException ex) {
						JOptionPane.showMessageDialog(new JFrame(),"Ingrese  un numero valido","Buscar y Retirar Estudiante",JOptionPane.WARNING_MESSAGE);
						return;
					}
				}
			};
		}
		return abstractAction5;
	}

}
